package cn.com.sina.rd.dsp.tools;


//import com.qq.jutil.sql.DBEngine;
//import com.qq.jutil.sql.DBFactory;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import java.security.MessageDigest;
import java.util.Random;
import java.util.TreeSet;

public class Utils {
	public static final Logger info_debug = Logger.getLogger("info_debug");//add by hao
//	/**
//	 * 获取admin数据库短连接
//	 */
//	public static DBEngine getAdminDBEngine() {
//		//return DBFactory.getDBEngine(ConfigUtil.isFormal() ? "conn_admin" : "conn_admin_test");
//		return DBFactory.getDBEngine("conn_admin_test");
//	}
//
//	/**
//	 * 获取book数据库短连接
//	 */
//	public static DBEngine getBookDBEngine() {
//		info_debug.info("--------Utils.getBookDBEngine:");//看是哪个db
//		//return DBFactory.getDBEngine(ConfigUtil.isFormal() ? "conn_book" : "conn_book_test");
//		return DBFactory.getDBEngine("conn_book");
//	}

    public static JSONObject getJson(String source){
        if(source!=null&&!source.isEmpty()){
            return JSONObject.fromObject(source);
        }else{
            return getJson();
        }
    }

    public static JSONObject getJson(){
        return getJson(0.5f);
    }

    public static JSONObject getJson(float ro){

        int serverNum=6;
        int loadNumPerServer=10;
        int loadWeigth=50;

        Random random=new Random();
        JSONObject josn=new JSONObject();
        josn.put("cluster_name","test1000");
        josn.put("high_gate",loadNumPerServer*loadWeigth*ro);
//        josn.put("high_gate",loadNumPerServer*loadWeigth*10);
        JSONArray servers=new JSONArray();
        //many server
        for(int i=0;i<serverNum;i++){
            JSONObject server_arr=new JSONObject();
            JSONArray region_arr=new JSONArray();
            int value=random.nextInt(loadNumPerServer)+1;
            //many region
            for(int j=0;j<value;j++){
                JSONObject single_load=new JSONObject();
                single_load.put("load_name", "load_" +i+"_"+ j);
                single_load.put("weight", random.nextInt(loadWeigth));
                region_arr.add(single_load);
            }
            server_arr.put("server_" + i, region_arr);
            servers.add(server_arr);
        }
        josn.put("servers",servers);
//        System.out.println(josn);
        return josn;
    }
	/*public static DBEngine getTTMDBEngine() {
		return DBFactory.getDBEngine("conn_ttm");
	}*/

    public static boolean NotNullOrEmpty(String value){
        return value!=null&&!value.isEmpty();
    }

    /**
     *
     * @param s
     * @return
     */
    public final static String MD5(String s) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     *  矩阵转置
     * @param obj
     * @return
     */
    public static JSONArray getServerList(JSONObject obj){
        TreeSet<String> set=new TreeSet<String>();
        JSONArray list=new JSONArray();
        JSONArray array= obj.getJSONArray("servers");
        for(int i=0;i<array.size();i++){
            JSONObject oneobj= array.getJSONObject(i);//{"server_0":[{"load_name":"region_00","weight":19},{"load_name":"region_01","weight":13}]}
            list.addAll(oneobj.keySet());
        }
        return list;
    }

}
